package com.swn.common.util;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharSetUtil {
	
	//加密
	/*
	public static String string2Unicode(String string) {
		StringBuffer unicode = new StringBuffer();
		for (int i = 0; i < string.length(); i++) {
			// 取出每一个字符
			char c = string.charAt(i);
			// 转换为unicode
			unicode.append("\\u" + Integer.toHexString(c));
		}
		return unicode.toString();
	}*/

	/**
	 * 对前台传过来的unicode进行解码    解密
	 * @param str
	 * @return String
	 * @Author 王万兴
	 * @Date 2017年9月2日 下午4:42:15 
	 * @company 济南舒卫能生物技术有限公司
	 */
	public static String decodeUnicode (String str) {
		Charset set = Charset.forName("UTF-16");
		Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
		Matcher m = p.matcher(str);
		int start = 0;
		int start2 = 0;
		StringBuffer sb = new StringBuffer();
		while (m.find(start)) {
			start2 = m.start();
			if (start2 > start) {
				String seg = str.substring(start, start2);
				sb.append(seg);
			}
			String code = m.group(1);
			int i = Integer.valueOf(code, 16);
			byte[] bb = new byte[4];
			bb[0] = (byte) ((i >> 8) & 0xFF);
			bb[1] = (byte) (i & 0xFF);
			ByteBuffer b = ByteBuffer.wrap(bb);
			sb.append(String.valueOf(set.decode(b)).trim());
			start = m.end();
		}
		start2 = str.length();
		if (start2 > start) {
			String seg = str.substring(start, start2);
			sb.append(seg);
		}
		return sb.toString();
	}

	
	
	
	/** 
	* 将字符串转成unicode  加密
	* @param str 待转字符串 
	* @return unicode字符串 
	*/ 
	public static String stringToUnicode(String str) {
		str = (str == null ? "" : str);
		String tmp;
		StringBuffer sb = new StringBuffer(1000);
		char c;
		int i, j;
		sb.setLength(0);
		for (i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			sb.append("\\u");
			j = (c >>> 8); // 取出高8位
			tmp = Integer.toHexString(j);
			if (tmp.length() == 1)
				sb.append("0");
			sb.append(tmp);
			j = (c & 0xFF); // 取出低8位
			tmp = Integer.toHexString(j);
			if (tmp.length() == 1)
				sb.append("0");
			sb.append(tmp);

		}
		return (new String(sb));
	}
	
	
	public static void main(String[] args) {
		//System.out.println(decodeUnicode("\u597d\u4eba"));
		//to unicode
		//System.out.println(stringToUnicode("{amount=3, commodity_url='http://192.168.4.17:8888/group1/M00/00/05/wKgEEVmCzT2EWHRHAAAAAMqwihw412.jpg', commodity_id=0ca326c502d94903802996aff6f721c6, specification_value_id='2,14,12', price=100.00, spe_name='10盒,红色,100ml', store_count=100, commodity_name='益生MT'}"));
		
		//System.out.println(stringToUnicode("【 顺丰包邮 6盒装】GABA自然醒γ-氨基丁酸180g氨基丁酸粉 给你每天好心情"));
		
		
		//to string 
		//System.out.println(decodeUnicode("\u007b\u0061\u006d\u006f\u0075\u006e\u0074\u003d\u0033\u002c\u0020\u0063\u006f\u006d\u006d\u006f\u0064\u0069\u0074\u0079\u005f\u0075\u0072\u006c\u003d\u0027\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u0031\u0039\u0032\u002e\u0031\u0036\u0038\u002e\u0034\u002e\u0031\u0037\u003a\u0038\u0038\u0038\u0038\u002f\u0067\u0072\u006f\u0075\u0070\u0031\u002f\u004d\u0030\u0030\u002f\u0030\u0030\u002f\u0030\u0035\u002f\u0077\u004b\u0067\u0045\u0045\u0056\u006d\u0043\u007a\u0054\u0032\u0045\u0057\u0048\u0052\u0048\u0041\u0041\u0041\u0041\u0041\u004d\u0071\u0077\u0069\u0068\u0077\u0034\u0031\u0032\u002e\u006a\u0070\u0067\u0027\u002c\u0020\u0063\u006f\u006d\u006d\u006f\u0064\u0069\u0074\u0079\u005f\u0069\u0064\u003d\u0030\u0063\u0061\u0033\u0032\u0036\u0063\u0035\u0030\u0032\u0064\u0039\u0034\u0039\u0030\u0033\u0038\u0030\u0032\u0039\u0039\u0036\u0061\u0066\u0066\u0036\u0066\u0037\u0032\u0031\u0063\u0036\u002c\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0063\u0061\u0074\u0069\u006f\u006e\u005f\u0076\u0061\u006c\u0075\u0065\u005f\u0069\u0064\u003d\u0027\u0032\u002c\u0031\u0034\u002c\u0031\u0032\u0027\u002c\u0020\u0070\u0072\u0069\u0063\u0065\u003d\u0031\u0030\u0030\u002e\u0030\u0030\u002c\u0020\u0073\u0070\u0065\u005f\u006e\u0061\u006d\u0065\u003d\u0027\u0031\u0030\u76d2\u002c\u7ea2\u8272\u002c\u0031\u0030\u0030\u006d\u006c\u0027\u002c\u0020\u0073\u0074\u006f\u0072\u0065\u005f\u0063\u006f\u0075\u006e\u0074\u003d\u0031\u0030\u0030\u002c\u0020\u0063\u006f\u006d\u006d\u006f\u0064\u0069\u0074\u0079\u005f\u006e\u0061\u006d\u0065\u003d\u0027\u76ca\u751f\u004d\u0054\u0027\u007d"));
	
		//System.out.println(decodeUnicode(stringToUnicode("【 顺丰包邮 6盒装】GABA自然醒γ-氨基丁酸180g氨基丁酸粉 给你每天好心情")));
		System.out.println(decodeUnicode("\\u03b3\\u002d"));
	}
}
